package mc0101.mccache.controller;

import lombok.extern.slf4j.Slf4j;
import mc0101.mccache.cache.TwoLevelLocalCache;
import mc0101.mccache.cache.ZcCacheLocalCache;
import mc0101.mccache.dto.ZcParamResponseData;
import mc0101.mccache.thread.LocalAndRemoteRefreshThread;
import mc0101.mccache.util.CacheInvalidJudgeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/zcParam")
public class ZcParamRefreshController{

    @Autowired
    private LocalAndRemoteRefreshThread localAndRemoteRefreshThread;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private ZcCacheLocalCache zcCacheLocalCache;

    @Autowired
    private TwoLevelLocalCache twoLevelLocalCache;

    @Autowired
    private CacheInvalidJudgeUtils cacheInvalidJudgeUtils;

//    @Autowired
//    private RefreshCacheBinding refreshCacheBinding;

    @RequestMapping(method = RequestMethod.GET, path = "/refresh")
    public ZcParamResponseData refresh(){
        threadPoolTaskExecutor.execute(localAndRemoteRefreshThread);
        return new ZcParamResponseData(true, "通知刷新成功！", null);
    }

    @RequestMapping(method = RequestMethod.GET, path = "/getParam")
    public ZcParamResponseData query(){
        long version = cacheInvalidJudgeUtils.getLocalCacheVersion();
        String param = zcCacheLocalCache.getCacheClassNameList()!=null ? zcCacheLocalCache.getCacheClassNameList()
                .toString() : "无";
        return new ZcParamResponseData(true, "本地版本为："+ version + "，缓存参数为：" + param,
                zcCacheLocalCache.getParam());
    }

    @RequestMapping(method = RequestMethod.GET, path = "/getTwoLevelCache")
    public Object queryTwoLevelCache(){
        return twoLevelLocalCache.getCache();
    }

    // 发布事件
//    @RequestMapping(method = RequestMethod.GET, path = "/refresh")
//    public ZcParamResponseData refreshAndPublishCacheRefreshEvent() {
//        log.info("接收到缓存刷新通知，生成缓存刷新事件，通知所有实例进行刷新！");
//
//        try {
//            String msg = "生成缓存刷新事件时间为：" + LocalDateTime.now();
//            refreshCacheBinding.output().send(MessageBuilder.withPayload(msg).build());
//        } catch (Exception e) {
//            log.error("生成缓存刷新事件失败，请检查日志以及MQ的健康情况！", e);
//            return new ZcParamResponseData(false, "通知刷新失败！", null);
//        }
//        return new ZcParamResponseData(true, "通知刷新成功！", null);
//    }
//
//    @StreamListener(target = RefreshCacheBinding.INPUT)
//    public void processHelloChannelGreeting(String msg) {
//        log.info("监听到缓存刷新事件-" + msg);
//        try {
//            threadPoolTaskExecutor.execute(localAndRemoteRefreshThread);
//        } catch (Exception ex){
//            log.error("缓存刷新线程执行失败，请观察是否已经有线程执行了，线程执行了请忽略该报错！", ex);
//        }
//    }
}